package com.obas.view.testplan.wizard {
    import com.ease.framework.CommandCompletionEvent;
    import com.ease.framework.ViewRegistry;
    import com.obas.business.taskexecution.vo.TestPlanObject;
    import com.obas.event.DataRefreshEvent;
    import com.obas.event.TestPlanOperationEvent;
    import com.obas.flex.common.util.ResourceUtil;
    import com.obas.model.DataRefreshHelper;
    import com.obas.model.business.TestPlan;
    import com.ease.component.controls.simplewizard.DialogWizard;
    import com.ease.component.managers.ConfirmHelper;
    import com.ease.component.managers.ErrorManager;

    import mx.collections.ArrayCollection;
    import mx.events.FlexEvent;
    import mx.managers.PopUpManager;
    import mx.utils.ObjectUtil;

    [Event(name="{com.obas.event.TestPlanOperationEvent.REQUEST_ID}", type="com.obas.event.TestPlanOperationEvent")]
    [Event(name="{com.obas.event.DataRefreshEvent.REQUEST_ID}", type="com.obas.event.DataRefreshEvent")]
    public class TestPlanWizard extends DialogWizard {
         private var _creationCallBack:Function;

         protected var _panels:Array = [];
         protected var _labels:Array = [];

         override public function TestPlanWizard() {
            showPath = false;
            resizable = true;

            width = 700;
            height = 500;
            var testplan:TestPlanObject = new TestPlanObject();
            testplan.testPlan = new TestPlan();
            userDefinedData = testplan;

            loadPanels();
            addEventListener(FlexEvent.PREINITIALIZE, creationCompleteHandler);
         }

         private function creationCompleteHandler(event:FlexEvent):void {
            ViewRegistry.instance.eventBus.addParticipant(this);
        }

        private function loadPanels():void {
           var nameStep:TestPlanNameView = new TestPlanNameView();
           _panels.push(nameStep);
           _labels.push("名称");

            var detailStep:TestPlanAddTestFunctionView = new TestPlanAddTestFunctionView();
            _panels.push(detailStep);
            _labels.push("选择功能点");

            var taskStep:TestPlanSetTaskView = new TestPlanSetTaskView();
            _panels.push(taskStep);
            _labels.push("设置任务");


            buildSimpleWizard(_panels, _labels);
        }

        override public function proceedFinish():void {
            loadingState = LOADING;
            dispatchEvent(new TestPlanOperationEvent([userDefinedData], TestPlanOperationEvent.CREATE_TEST_PLAN));
        }

        [ResponseHandler("{com.obas.event.TestPlanOperationEvent.RETURN_ID}")]
        public function onTestPlanOperationEvent(event:CommandCompletionEvent):void {
           loadingState = LOADED;
           if (event.error) {
              ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
              return ;
           }
           ConfirmHelper.showInformationDialog("", "新建测试计划成功");

           var helper:DataRefreshHelper = new DataRefreshHelper();
           var lists:ArrayCollection = new ArrayCollection;
           lists.addItem(event.result);
           helper.data = lists;
           dispatchEvent(new DataRefreshEvent(helper, DataRefreshEvent.TYPE_ADD_UPDATE));
           PopUpManager.removePopUp(this);
        }

    }
}